package com.bsoft.wx;

import cn.hutool.core.util.StrUtil;
import com.bsoft.wx.constant.SystemConstans;
import com.bsoft.wx.pojo.SysConfig;
import com.bsoft.wx.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @author hdh
 * @ Data   :Created in 22:25 2023/1/11
 * @ Description 启动类
 * @ Modified By: hdh
 * @ Version : 1.0
 */
@ComponentScan(basePackages = "com.bsoft")
@MapperScan("com.bsoft.wx.mapper")
@SpringBootApplication
@ServletComponentScan  //跨站脚本
@Slf4j
@EnableAsync
public class emosApplication {
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private SystemConstans systemConstans;

    public static void main(String[] args) {
        SpringApplication.run(emosApplication.class, args);
    }

    //初始化
    @PostConstruct
    public void init() {
        List<SysConfig> configList = sysConfigService.selectAllParam();
        configList.forEach(one -> {
            String key = one.getParamKey();
            key = StrUtil.toCamelCase(key);
            String value = one.getParamValue();
            try {
                Field field = systemConstans.getClass().getDeclaredField(key);
                field.set(systemConstans, value);
            } catch (Exception e) {
                log.info("执行异常", e);
            }


        });
    }
}
